

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>AG_image_tools.drizzle &mdash; agpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/extra.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1.2 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
    <link rel="stylesheet" type="text/css" href="../../_static/extra.css" />
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1.2 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for AG_image_tools.drizzle</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">numpy</span>

<div class="viewcode-block" id="masktozero"><a class="viewcode-back" href="../../image_tools.html#AG_image_tools.drizzle.masktozero">[docs]</a><span class="k">def</span> <span class="nf">masktozero</span><span class="p">(</span><span class="n">arr</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convert all NANs or masked elements of an array to zero, and return a numpy</span>
<span class="sd">    array (NOT a masked array).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span><span class="s">&#39;mask&#39;</span><span class="p">):</span>
        <span class="n">arr</span><span class="p">[</span><span class="n">arr</span><span class="o">.</span><span class="n">mask</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">arr</span><span class="p">[</span><span class="n">arr</span><span class="o">!=</span><span class="n">arr</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="drizzle"><a class="viewcode-back" href="../../image_tools.html#AG_image_tools.drizzle.drizzle">[docs]</a><span class="k">def</span> <span class="nf">drizzle</span><span class="p">(</span><span class="n">tstomap</span><span class="p">,</span><span class="n">ts</span><span class="p">,</span><span class="n">mapshape</span><span class="p">,</span><span class="n">weights</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">weightmap</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Drizzle a timestream onto a map.  Returns the map of the weighted average</span>
<span class="sd">    per pixel of the input timestream.</span>
<span class="sd">    (note that this works for any 1D array with a same-size mapping to an</span>
<span class="sd">    image; I&#39;ve written it with timestreams in mind though)</span>

<span class="sd">    tstomap - mapping from timestream -&gt; map.  len(tstomap) = len(ts)</span>
<span class="sd">        Both tstomap and ts should be one-dimensional, but they&#39;ll be raveled</span>
<span class="sd">        if you don&#39;t do it yourself</span>
<span class="sd">    weights - needs to have the same dimensions as ts *or* be scalar</span>
<span class="sd">            (default=1)</span>
<span class="sd">    mapshape - [nx,ny] simple 2D map specification.  Make sure your map</span>
<span class="sd">        includes all points mapped to</span>

<span class="sd">    You can specify a weightmap to increase efficiency instead of computing it</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">newmap</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">mapshape</span><span class="p">)</span>
    <span class="c"># don&#39;t need to mask out when adding zero; simplifies indexing to have no masks</span>
    <span class="n">ts_to_index</span> <span class="o">=</span> <span class="n">masktozero</span><span class="p">((</span><span class="n">ts</span><span class="o">*</span><span class="n">weights</span><span class="p">)</span><span class="o">.</span><span class="n">ravel</span><span class="p">())</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tstomap</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span> <span class="c"># tstomap must be flat</span>
        <span class="n">tstomap</span> <span class="o">=</span> <span class="n">tstomap</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>

    <span class="n">tsmapped</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">tstomap</span><span class="p">,</span><span class="n">ts_to_index</span><span class="p">)</span>

    <span class="c"># bincount has length = argmax(tstomap), but the x/y index arrays</span>
    <span class="c"># go all the way to the full map size.  Append zeros to fill</span>
    <span class="n">maxind</span> <span class="o">=</span> <span class="n">mapshape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">mapshape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">tsmapped</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">maxind</span><span class="p">:</span>
        <span class="n">tsmapped</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">tsmapped</span><span class="p">,</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">maxind</span><span class="o">-</span><span class="n">tsmapped</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])])</span>
    <span class="n">xinds</span><span class="p">,</span><span class="n">yinds</span> <span class="o">=</span> <span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">numpy</span><span class="o">.</span><span class="n">indices</span><span class="p">(</span><span class="n">mapshape</span><span class="p">))</span>
    <span class="n">newmap</span><span class="p">[</span><span class="n">xinds</span><span class="p">,</span><span class="n">yinds</span><span class="p">]</span> <span class="o">=</span> <span class="n">tsmapped</span>

    <span class="c"># do the same for weights unless a weightmap is specified</span>
    <span class="k">if</span> <span class="n">weightmap</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> 
        <span class="n">wm</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">mapshape</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">isscalar</span><span class="p">(</span><span class="n">weights</span><span class="p">):</span> 
            <span class="n">weights_to_index</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">ts_to_index</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span><span class="o">*</span><span class="n">weights</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">weights_to_index</span> <span class="o">=</span> <span class="n">masktozero</span><span class="p">((</span><span class="n">weights</span><span class="p">)</span><span class="o">.</span><span class="n">ravel</span><span class="p">())</span>

        <span class="n">tsweights</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">tstomap</span><span class="p">,</span><span class="n">weights_to_index</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">tsweights</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">maxind</span><span class="p">:</span>
            <span class="n">tsweights</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">tsweights</span><span class="p">,</span>
                <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">maxind</span><span class="o">-</span><span class="n">tsweights</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])])</span>
        <span class="n">wm</span><span class="p">[</span><span class="n">xinds</span><span class="p">,</span><span class="n">yinds</span><span class="p">]</span> <span class="o">=</span> <span class="n">tsweights</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">wm</span> <span class="o">=</span> <span class="n">weightmap</span>

    <span class="k">return</span> <span class="n">newmap</span><span class="o">/</span><span class="n">wm</span></div>
</pre></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>